home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 4 / Example 4.6 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  7.1 KB  |  264 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 4.6: Pre-rendered Textures                        //
  3. // Written by: C. Granberg, 2005                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include <vector>
  9. #include "debug.h"
  10. #include "heightMap.h"
  11. #include "terrain.h"
  12.  
  13. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  14. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  15.  
  16. class APPLICATION
  17. {
  18.     public:
  19.         APPLICATION();
  20.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  21.         HRESULT Update(float deltaTime);
  22.         HRESULT Render();
  23.         HRESULT Cleanup();
  24.         HRESULT Quit();
  25.  
  26.     private:
  27.         IDirect3DDevice9* m_pDevice; 
  28.         TERRAIN m_terrain;
  29.  
  30.         float m_angle, m_radius;
  31.         bool m_wireframe;
  32.         D3DLIGHT9 m_light;
  33.         HWND m_mainWindow;
  34.         ID3DXFont *m_pFont;
  35. };
  36.  
  37. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  38. {
  39.     APPLICATION app;
  40.  
  41.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  42.         return 0;
  43.  
  44.     MSG msg;
  45.     memset(&msg, 0, sizeof(MSG));
  46.     int startTime = timeGetTime(); 
  47.  
  48.     while(msg.message != WM_QUIT)
  49.     {
  50.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  51.         {
  52.             ::TranslateMessage(&msg);
  53.             ::DispatchMessage(&msg);
  54.         }
  55.         else
  56.         {    
  57.             int t = timeGetTime();
  58.             float deltaTime = (t - startTime)*0.001f;
  59.  
  60.             app.Update(deltaTime);
  61.             app.Render();
  62.  
  63.             startTime = t;
  64.         }
  65.     }
  66.  
  67.     app.Cleanup();
  68.  
  69.     return msg.wParam;
  70. }
  71.  
  72. APPLICATION::APPLICATION()
  73. {
  74.     m_pDevice = NULL; 
  75.     m_mainWindow = 0;
  76.     m_angle = 0.0f;
  77.     m_radius = 100.0f;
  78.     m_wireframe = false;
  79.  
  80.     srand(GetTickCount());
  81. }
  82.  
  83. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  84. {
  85.     debug.Print("Application initiated");
  86.  
  87.     //Create Window Class
  88.     WNDCLASS wc;
  89.     memset(&wc, 0, sizeof(WNDCLASS));
  90.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  91.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  92.     wc.hInstance     = hInstance;
  93.     wc.lpszClassName = "D3DWND";
  94.  
  95.     //Register Class and Create new Window
  96.     RegisterClass(&wc);
  97.     m_mainWindow = CreateWindow("D3DWND", "Example 4.6: Pre-rendered Textures", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  98.     SetCursor(NULL);
  99.     ShowWindow(m_mainWindow, SW_SHOW);
  100.     UpdateWindow(m_mainWindow);
  101.  
  102.     //Create IDirect3D9 Interface
  103.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  104.  
  105.     if(d3d9 == NULL)
  106.     {
  107.         debug.Print("Direct3DCreate9() - FAILED");
  108.         return E_FAIL;
  109.     }
  110.  
  111.     //Check that the Device supports what we need from it
  112.     D3DCAPS9 caps;
  113.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  114.  
  115.     //Hardware Vertex Processing or not?
  116.     int vp = 0;
  117.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  118.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  119.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  120.  
  121.     //Check vertex & pixelshader versions
  122.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  123.     {
  124.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  125.     }
  126.  
  127.     //Set D3DPRESENT_PARAMETERS
  128.     D3DPRESENT_PARAMETERS d3dpp;
  129.     d3dpp.BackBufferWidth            = width;
  130.     d3dpp.BackBufferHeight           = height;
  131.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  132.     d3dpp.BackBufferCount            = 1;
  133.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  134.     d3dpp.MultiSampleQuality         = 0;
  135.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  136.     d3dpp.hDeviceWindow              = m_mainWindow;
  137.     d3dpp.Windowed                   = windowed;
  138.     d3dpp.EnableAutoDepthStencil     = true; 
  139.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  140.     d3dpp.Flags                      = 0;
  141.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  142.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  143.  
  144.     //Create the IDirect3DDevice9
  145.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  146.                                  vp, &d3dpp, &m_pDevice)))
  147.     {
  148.         debug.Print("Failed to create IDirect3DDevice9");
  149.         return E_FAIL;
  150.     }
  151.  
  152.     //Release IDirect3D9 interface
  153.     d3d9->Release();
  154.  
  155.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  156.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  157.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  158.  
  159.     //Create Terrain
  160.     m_terrain.Init(m_pDevice, INTPOINT(100,100));
  161.  
  162.     //Create m_light
  163.     ::ZeroMemory(&m_light, sizeof(m_light));
  164.     m_light.Type      = D3DLIGHT_DIRECTIONAL;
  165.     m_light.Ambient   = D3DXCOLOR(0.5, 0.5, 0.5, 1.0f);
  166.     m_light.Diffuse   = D3DXCOLOR(0.9, 0.9, 0.9, 1.0f);
  167.     m_light.Specular  = D3DXCOLOR(0.5, 0.5, 0.5, 1.0f);
  168.     m_light.Direction = D3DXVECTOR3(0.7f, -0.3f, 0.0f);
  169.     m_pDevice->SetLight(0, &m_light);
  170.     m_pDevice->LightEnable(0, true);
  171.  
  172.     //Set sampler state
  173.     m_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  174.     m_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  175.     m_pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  176.  
  177.     return S_OK;
  178. }
  179.  
  180. HRESULT APPLICATION::Update(float deltaTime)
  181. {
  182.     //Control camera
  183.     m_angle += deltaTime * 0.5f;
  184.     D3DXMATRIX  matWorld, matView, matProj;        
  185.     D3DXVECTOR2 centre = D3DXVECTOR2(50, 50);
  186.     D3DXVECTOR3 Eye    = D3DXVECTOR3(centre.x + cos(m_angle) * m_radius, m_radius, -centre.y + sin(m_angle) * m_radius);
  187.     D3DXVECTOR3 Lookat = D3DXVECTOR3(centre.x, 0.0f,  -centre.y);
  188.  
  189.     D3DXMatrixIdentity(&matWorld);
  190.     D3DXMatrixLookAtLH(&matView, &Eye, &Lookat, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  191.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.3333f, 1.0f, 1000.0f );
  192.  
  193.     m_pDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  194.     m_pDevice->SetTransform( D3DTS_VIEW,       &matView );
  195.     m_pDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  196.     
  197.     if(KEYDOWN('W'))
  198.     {
  199.         m_wireframe = !m_wireframe;
  200.         Sleep(300);
  201.     }
  202.     else if(KEYDOWN(VK_ADD) && m_radius < 200.0f)
  203.     {
  204.         m_radius += deltaTime * 30.0f;
  205.     }
  206.     else if(KEYDOWN(VK_SUBTRACT) && m_radius > 5.0f)
  207.     {
  208.         m_radius -= deltaTime * 30.0f;
  209.     }
  210.  
  211.     if(KEYDOWN(VK_ESCAPE))
  212.         Quit();
  213.  
  214.     return S_OK;
  215. }    
  216.  
  217. HRESULT APPLICATION::Render()
  218. {
  219.     // Clear the viewport
  220.     m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );
  221.  
  222.     // Begin the scene 
  223.     if(SUCCEEDED(m_pDevice->BeginScene()))
  224.     {
  225.         if(m_wireframe)m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);    
  226.         else m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  227.  
  228.         m_terrain.Render();
  229.  
  230.         RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}};
  231.         m_pFont->DrawText(NULL, "W: Toggle Wireframe", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  232.         m_pFont->DrawText(NULL, "+/-: Zoom In/Out", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  233.  
  234.         // End the scene.
  235.         m_pDevice->EndScene();
  236.         m_pDevice->Present(0, 0, 0, 0);
  237.     }
  238.  
  239.     return S_OK;
  240. }
  241.  
  242. HRESULT APPLICATION::Cleanup()
  243. {
  244.     try
  245.     {
  246.         m_terrain.Release();
  247.  
  248.         m_pFont->Release();
  249.         m_pDevice->Release();
  250.  
  251.         debug.Print("Application terminated");
  252.     }
  253.     catch(...){}
  254.  
  255.     return S_OK;
  256.  
  257. }
  258.  
  259. HRESULT APPLICATION::Quit()
  260. {
  261.     ::DestroyWindow(m_mainWindow);
  262.     ::PostQuitMessage(0);
  263.     return S_OK;
  264. }